package com.flyco.modules.system.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.common.api.vo.Result;
import com.flyco.common.system.query.QueryGenerator;
import com.flyco.common.aspect.annotation.AutoLog;
import com.flyco.common.util.oConvertUtils;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import com.flyco.modules.system.entity.SysStoreUser;
import com.flyco.modules.system.entity.SysUser;
import com.flyco.modules.system.service.ISysStoreUserService;

import java.util.Date;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyco.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import com.flyco.common.system.base.controller.BaseController;
import com.flyco.poi.excel.ExcelImportUtil;
import com.flyco.poi.excel.def.NormalExcelConstants;
import com.flyco.poi.excel.entity.ExportParams;
import com.flyco.poi.excel.entity.ImportParams;
import com.flyco.poi.excel.view.EntityExcelView;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 经销商跟单员关系表
 * @Author: flyco
 * @Date: 2024-05-14
 * @Version: V1.0
 */
@Slf4j
@Api(tags = "经销商跟单员关系表")
@RestController
@RequestMapping("/f/system/sysStoreUser")
public class SysStoreUserController extends BaseController<SysStoreUser, ISysStoreUserService> {
    @Autowired
    private ISysStoreUserService sysStoreUserService;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param sysStoreUser
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-分页列表查询")
    @ApiOperation(value = "经销商跟单员关系表-分页列表查询", notes = "经销商跟单员关系表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SysStoreUser sysStoreUser,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<SysStoreUser> queryWrapper = QueryGenerator.initQueryWrapper(sysStoreUser, req.getParameterMap());
        Page<SysStoreUser> page = new Page<SysStoreUser>(pageNo, pageSize);
        IPage<SysStoreUser> pageList = sysStoreUserService.page(page, queryWrapper);
        pageList.getRecords().forEach(item -> {
            LambdaQueryWrapper<StmStoreInfo> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(StmStoreInfo::getCompanyCode, item.getStoreId());
            List<StmStoreInfo> storeInfos = stmStoreInfoService.list(queryWrapper1);
            if (!CollectionUtils.isEmpty(storeInfos)) {
                item.setStoreName(storeInfos.get(0).getName());
            }
            LambdaQueryWrapper<SysUser> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(SysUser::getUsername, item.getUserName());
            List<SysUser> userList = sysUserService.list(queryWrapper2);
            if (!CollectionUtils.isEmpty(userList)) {
                item.setRealName(userList.get(0).getRealname());
            }
        });
        return Result.ok(pageList);
    }

    /**
     * 添加
     *
     * @param sysStoreUser
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-添加")
    @ApiOperation(value = "经销商跟单员关系表-添加", notes = "经销商跟单员关系表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysStoreUser sysStoreUser) {
        sysStoreUserService.saveWithUser(sysStoreUser);
        return Result.ok("添加成功！");
    }

    /**
     * 编辑
     *
     * @param sysStoreUser
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-编辑")
    @ApiOperation(value = "经销商跟单员关系表-编辑", notes = "经销商跟单员关系表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody SysStoreUser sysStoreUser) {
        sysStoreUserService.saveWithUser(sysStoreUser);
        return Result.ok("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-通过id删除")
    @ApiOperation(value = "经销商跟单员关系表-通过id删除", notes = "经销商跟单员关系表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysStoreUserService.removeById(id);
        return Result.ok("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-批量删除")
    @ApiOperation(value = "经销商跟单员关系表-批量删除", notes = "经销商跟单员关系表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.sysStoreUserService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "经销商跟单员关系表-通过id查询")
    @ApiOperation(value = "经销商跟单员关系表-通过id查询", notes = "经销商跟单员关系表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysStoreUser sysStoreUser = sysStoreUserService.getById(id);
        LambdaQueryWrapper<StmStoreInfo> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(StmStoreInfo::getStoreId, sysStoreUser.getStoreId());
        List<StmStoreInfo> storeInfos = stmStoreInfoService.list(queryWrapper1);
        if (!CollectionUtils.isEmpty(storeInfos)) {
            sysStoreUser.setStoreName(storeInfos.get(0).getName());
        }
        return Result.ok(sysStoreUser);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param sysStoreUser
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysStoreUser sysStoreUser) {
        return super.exportXls(request, sysStoreUser, SysStoreUser.class, "经销商跟单员关系表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        super.importExcel(request, response, SysStoreUser.class);
        sysStoreUserService.importData();
        return Result.ok("导入成功！");
    }

    /**
     * 修改经销商跟单员关系
     *
     * @param sysStoreUser
     * @return
     */
    @AutoLog(value = "修改经销商跟单员关系表")
    @ApiOperation(value = "修改经销商跟单员关系表", notes = "修改经销商跟单员关系表")
    @PutMapping(value = "/updateStoreUser")
    public Result<?> updateStoreUser(@RequestBody SysStoreUser sysStoreUser) {
        sysStoreUserService.updateStoreUser(sysStoreUser);
        return Result.ok("修改跟单员关系成功!");
    }

}
